/**
 * Copyright (c) 2008 riaevolution.com, All Rights Reserved.
 *
 * See www.riaevolution.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * */
package com.riaevolution.components {
	import flash.display.Graphics;

	import mx.containers.FormHeading;
	import mx.utils.ColorUtil;

	[Style(name="lineHeight", type = "Number", format = "Length")]
	[Style(name="lineAlpha", type = "Number", format = "Length")]
	[Style(name="lineColor", type = "uint", format = "Color")]
	/* [Style(name="shadowColor", type = "uint", format = "Color")] */

	/**
	 * The FormHeadinLined provide a nice way to create a line below the text.
	 * You create good locking form with this componente.
	 * The line follow the bottom of the componente.
	 * <pre>
	 * 	<strong>Styles</strong>
	 * 	lineHeight="8"
	 *  lineAlpha="1"
	 *  lineColor="#EFEFEF"
	 * </pre>
	 *
	 *
	 * <br /><b>Luciano bustos</b> <a href="mailto:luchyx&#64;riaevolution.com">luchyx&#64;riaevolution.com</a><br /><a href="http://www.riaevolution.com">www.riaevolution.com</a>
	 */
	public class FormHeadingLined extends FormHeading {

		public function FormHeadingLined() {
			super();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledWidth);
			/* var g:Graphics = this.graphics;
			 g.clear(); */

			var col:Number = this.getStyle("lineColor");
			col = isNaN(col) ? this.getStyle("color") : col;

			var lineh:Number = this.getStyle("lineHeight");
			lineh = isNaN(lineh) ? 2 : lineh;

			var ap:Number = this.getStyle("lineAlpha");
			ap = isNaN(ap) ? 1 : ap;

			//shadowColor
			var col2:Number = this.getStyle("shadowColor");
			if (isNaN(col2)) {
				col2 = ColorUtil.adjustBrightness2(col, 100);
			}

			drawLine(col, col2, ap, unscaledWidth, unscaledHeight, lineh);

		/* g.beginFill(col, ap);
		   g.drawRect(0, unscaledHeight - lineh, unscaledWidth, lineh);
		 g.endFill(); */
		}

		private function drawLine(strokeColor:Number, shadowColor:Number, alpha:Number, w:Number, h:Number, lineHeight:Number = 2):void {
			var g:Graphics = graphics;

			g.clear();

			// Look up the style properties
			//var strokeColor:Number = strokeColor; //getStyle("strokeColor");
			//var shadowColor:Number = shadowColor; //getStyle("shadowColor");
			var strokeWidth:Number = 2; //getStyle("strokeWidth");
			// The thickness of the stroke shouldn't be greater than
			// the unscaledHeight of the bounding rectangle.
			/* 	if (strokeWidth > unscaledHeight)
			 strokeWidth = unscaledHeight; */

			// The horizontal rule extends from the left edge
			// to the right edge of the bounding rectangle and
			// is vertically centered within the bounding rectangle.
			var left:Number = 0;
			var top:Number = 0;
			var right:Number = w;
			var bottom:Number = h;

			if (strokeWidth == 1) {
				// **************

				g.beginFill(strokeColor);
				g.drawRect(left, top, unscaledWidth, height);
				g.endFill();
			} else if (strokeWidth == 2) {
				// **************
				// oooooooooooooo

				g.beginFill(strokeColor, alpha);
				//x,y,w,h
				g.drawRect(left, h - lineHeight, w, lineHeight);
				g.endFill();

				g.beginFill(shadowColor, alpha);
				g.drawRect(left, h - lineHeight / 2, w, lineHeight / 2);
				g.endFill();
			} else if (strokeWidth > 2) {
				// *************o
				// *            o
				// oooooooooooooo

				g.beginFill(strokeColor);
				g.drawRect(left, top, unscaledWidth - 1, 1);
				g.endFill();

				g.beginFill(shadowColor);
				g.drawRect(right - 1, top, 1, bottom - top - 1);
				g.drawRect(left, bottom - 1, unscaledWidth, 1);
				g.endFill();

				g.beginFill(strokeColor);
				g.drawRect(left, top + 1, 1, bottom - top - 2);
				g.endFill();
			}
		}
	}
}